<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: Lk
 * @Date: 2023-06-26 15:32:33
 * @LastEditors: Lk
 * @LastEditTime: 2023-07-03 11:02:32
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\Cart as ModelCart;
use app\common\model\forest\Member;
use app\common\model\Goods;
use app\common\model\goods\Cart as GoodsCart;
use think\cache\driver\Redis;

/**
 * 购物车接口
 */
class Cart extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = ['*'];


    /**
     * 添加/编辑购物车
     * @ApiMethod (POST)
     * @param string $goods_id  商品id
     * @param int $num  商品数量
     * @param int $type  类型:1=购物车购买,2=立即购买
     */
    public function addCart()
    {
        $param = $this->request->post();
        $param['uid'] = $this->auth->id;
        try {
            $res = GoodsCart::addCart($param);
        } catch (\Exception $e) {
            $this->error('失败:' . $e->getMessage());
        }
        $this->success('成功', $res);
    }
    /**
     * 删除购物车
     * @ApiMethod (POST)
     *
     */
    public function delCart()
    {
        $res = GoodsCart::delCart($this->auth->id);
        if ($res) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
    /**
     * 查看购物车
     * @ApiMethod (get)
     */
    public function cartList()
    {
        $uid = $this->auth->id;
        $data = GoodsCart::list($uid);
        $cloumn = array_column($data, 'num');
        $this->success('获取成功', ['data' => $data, 'count' => array_sum($cloumn)]);
    }
}
